﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using jiaju.Models;
using jiaju.Models.DBModel;
namespace jiaju.Models.DAL
{
    public class ProductService
    {
        public static List<Product> GetProduct()
        {
            string sql = "select Id, Name, ImgUrl from dbo.Product ";
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            List<Product> list = new List<Product>();
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                string ImgUrl = (string)sdr["ImgUrl"];
                Product ban = new Product()
                {
                    Id = Id,
                    Name = Name,
                    ImgUrl = ImgUrl
                };
                list.Add(ban);
            }
            return list;
        }
        public static List<Product> GetTypeProduct(string Id)
        {
            string sql = "select Id, Name, ImgUrl from dbo.Product where ProductTypeId=" + Id;
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            List<Product> list = new List<Product>();
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                string ImgUrl = (string)sdr["ImgUrl"];
                Product ban = new Product()
                {
                    Id = id,
                    Name = Name,
                    ImgUrl = ImgUrl
                };
                list.Add(ban);
            }
            return list;
        }
        public static Product GetProductList(string Id)
        {
            string sql = string.Format("select * from dbo.Product where Id={0}", Id);
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            Product list = null;
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string CaiZhi = (string)sdr["CaiZhi"];
                Decimal ProductPrice = (Decimal)sdr["ProductPrice"];
                Decimal MarketPrice = (Decimal)sdr["MarketPrice"];
                string Brand = (string)sdr["Brand"];
                string XingHao = (string)sdr["XingHao"];
                string FengGe = (string)sdr["FengGe"];
                string ZuZhuang = (string)sdr["ZuZhuang"];
                string Color = (string)sdr["Color"];
                string KuanShi = (string)sdr["KuanShi"];
                string HomeFlag = (string)sdr["HomeFlag"];
                string Cont = (string)sdr["Cont"];
                list = new Product()
                {
                    Id = id,
                    Name = Name,
                    ImgUrl = ImgUrl,
                    CaiZhi = CaiZhi,
                    ProductPrice = ProductPrice,
                    MarketPrice = MarketPrice,
                    Brand = Brand,
                    XingHao = XingHao,
                    FengGe = FengGe,
                    ZuZhuang = ZuZhuang,
                    Color = Color,
                    KuanShi = KuanShi,
                    HomeFlag = HomeFlag,
                    Cont = Cont
                };
            }
            return list;
        }
        public static List<Product> GetAllProduct(int page, int limit, string name)
        {
            string sql = string.Format("select top({1})* from dbo.Product where Id not in (select top(({0}-1)*{1})Id from dbo.Product)", page, limit);
            if (!String.IsNullOrEmpty(name))
            {
                sql = sql + " and name like '%" + name + "%'";
            }
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            List<Product> list = new List<Product>();
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string CaiZhi = (string)sdr["CaiZhi"];
                Decimal ProductPrice = (Decimal)sdr["ProductPrice"];
                Decimal MarketPrice = (Decimal)sdr["MarketPrice"];
                string Brand = (string)sdr["Brand"];
                string XingHao = (string)sdr["XingHao"];
                string FengGe = (string)sdr["FengGe"];
                string ZuZhuang = (string)sdr["ZuZhuang"];
                string Color = (string)sdr["Color"];
                string KuanShi = (string)sdr["KuanShi"];
                string HomeFlag = (string)sdr["HomeFlag"];
                string Cont = (string)sdr["Cont"];
                Product li = new Product()
                {
                    Id = id,
                    Name = Name,
                    ImgUrl = ImgUrl,
                    CaiZhi = CaiZhi,
                    ProductPrice = ProductPrice,
                    MarketPrice = MarketPrice,
                    Brand = Brand,
                    XingHao = XingHao,
                    FengGe = FengGe,
                    ZuZhuang = ZuZhuang,
                    Color = Color,
                    KuanShi = KuanShi,
                    HomeFlag = HomeFlag,
                    Cont = Cont
                };
                list.Add(li);
            }
            return list;
        }
        public static List<Product> GetTypeProduct(int page, int limit, string TypeId, string name)
        {
            string sql = string.Format("select top({1})* from dbo.Product where Id not in (select top(({0}-1)*{1})Id from dbo.Product) and ProductTypeId={2}", page, limit, TypeId);
            if (!String.IsNullOrEmpty(name))
            {
                sql = sql + " and name like '%" + name + "%'";
            }
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            List<Product> list = new List<Product>();
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                string ImgUrl = (string)sdr["ImgUrl"];
                string CaiZhi = (string)sdr["CaiZhi"];
                Decimal ProductPrice = (Decimal)sdr["ProductPrice"];
                Decimal MarketPrice = (Decimal)sdr["MarketPrice"];
                string Brand = (string)sdr["Brand"];
                string XingHao = (string)sdr["XingHao"];
                string FengGe = (string)sdr["FengGe"];
                string ZuZhuang = (string)sdr["ZuZhuang"];
                string Color = (string)sdr["Color"];
                string KuanShi = (string)sdr["KuanShi"];
                string HomeFlag = (string)sdr["HomeFlag"];
                string Cont = (string)sdr["Cont"];
                Product li = new Product()
                {
                    Id = id,
                    Name = Name,
                    ImgUrl = ImgUrl,
                    CaiZhi = CaiZhi,
                    ProductPrice = ProductPrice,
                    MarketPrice = MarketPrice,
                    Brand = Brand,
                    XingHao = XingHao,
                    FengGe = FengGe,
                    ZuZhuang = ZuZhuang,
                    Color = Color,
                    KuanShi = KuanShi,
                    HomeFlag = HomeFlag,
                    Cont = Cont
                };
                list.Add(li);
            }
            return list;
        }
    }
}